#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+7,N0=2007;
int T,n,a[N];
ll dp[N0][2][N0],cnt[N];

void solve1(){
    //cerr<<n<<" solve1"<<endl;//
    for(int i=1;i<=n;i++){
        for(int k=1;k<i;k++) dp[i][0][k]=dp[i][1][k]=0;
        for(int k=1;k<i;k++){
            //same
            dp[i][0][k]=max(dp[i][0][k],dp[i-1][0][k]+(a[i]==a[i-1])*(ll)a[i]);
            dp[i][1][k]=max(dp[i][1][k],dp[i-1][1][k]+(a[i]==a[i-1])*(ll)a[i]);
            if(k<i-1){
                //diff
                //...10,k=last 0
                dp[i][0][i-1]=max(dp[i][0][i-1],dp[i-1][1][k]+(a[i]==a[k])*(ll)a[i]);
                //...01,k=last 1
                dp[i][1][i-1]=max(dp[i][1][i-1],dp[i-1][0][k]+(a[i]==a[k])*(ll)a[i]);
            }
        }
    }
    ll ans=0;
    for(int k=1;k<n;k++) ans=max(ans,max(dp[n][0][k],dp[n][1][k]));
    printf("%lld\n",ans);
}

void solve2(){
    //cerr<<n<<" solve2"<<endl;//
    memset(cnt,0,sizeof(cnt));
    for(int i=1;i<=n;i++) cnt[a[i]]+=a[i];
    int idx=1;
    for(int i=2;i<N;i++)
        if(cnt[i]>cnt[idx]) idx=i;
    ll mx=0;
    for(int i=1;i<=n;i++)
        if(i!=idx) mx=max(mx,cnt[i]);
    printf("%lld\n",cnt[idx]+mx);
}

int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",a+i);
        if(n<N0) solve1();
        else solve2();
    }
    return 0;
}
